/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import entity.Applicant;
import entity.Interview;
import entity.Vacancy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ApplicantModel;
import model.InterviewModel;
import model.VacancyModel;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import utility.SendMail;

/**
 *
 * @author Lab
 */
public class viewer_updateItvAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        InterviewModel im = new InterviewModel();
        Interview i = new Interview();
        i.setApplicantid(request.getParameter("hdApplicantid"));
        i.setVacancyid(request.getParameter("hdVacancyid"));
        i.setEmpid(request.getParameter("cbxEmployee"));
        i.setScheduleDate(request.getParameter("scheduleDate"));
        i.setStartTime(request.getParameter("startTime"));
        i.setEndTime(request.getParameter("endTime"));
        i.setStatus(request.getParameter("cbxStatus"));
        i.setCommand(request.getParameter("command"));

        String statusItv = request.getParameter("cbxStatus");
        String aid = request.getParameter("hdApplicantid");
        String vid = request.getParameter("hdVacancyid");

        ApplicantModel am = new ApplicantModel();
        Applicant a = am.getOneApplicant(request.getParameter("hdApplicantid"));
        VacancyModel vm = new VacancyModel();
        Vacancy v = vm.getOneVacancy(request.getParameter("hdVacancyid"));
        String toEmail = a.getApplicantEmail();
        String subject = "";
        String content = "";

        if (im.updateInterview(i)) {
            
            if (statusItv.equals("Scheduled")) {
                am.updateStatusApplicant("Inprocess", aid);

                subject = "Invitation Interview";
                content = "Vacancy name: " + v.getVacancyTitle() + "\n\n"
                            + "Applicant name: " + a.getApplicantName() + "\n\n"
                            + "Schduled date: " + i.getScheduleDate() + "\n\n"
                            + "Start time: " + i.getStartTime() + "\n\n"
                            + "End time: " + i.getEndTime() + "\n\n";
            }
            else if(statusItv.equals("Selected")){
                am.updateStatusApplicant("Hired", aid);

                subject = "Final Result Of Interview";
                content = "Vacancy name: " + v.getVacancyTitle() + "\n\n"
                            + "Applicant name: " + a.getApplicantName() + "\n\n"
                            + "Result: congratulation you were selected.\n\n \tYou can contact with company to work.";
            }
            else if(statusItv.equals("Rejected") || statusItv.equals("Not Required")){
                am.updateStatusApplicant("Not Inprocess", aid);
            }

            SendMail sm = new SendMail();
            sm.send(toEmail, subject, content);

            int numofjob = vm.getNumofjob(vid);
            int countItv = im.getStatusItv(vid);
            if (numofjob == countItv){
                vm.updateStatusVacancy(vid, "Close");
            }
            
            return mapping.findForward("ok");
        }

        return mapping.findForward("fail");
    }
}
